package com.colin.app.activity.view.materialdesign;

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.colin.app.R;
import com.colin.app.activity.view.ViewContract;
import com.colin.app.activity.view.ViewPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.app.fragment.recyclerview.RecyclerViewFragment;
import com.colin.library.base.BaseFragment;
import com.colin.library.help.LogHelp;
import com.colin.library.other.Constants;

public class MaterialDesignActivity extends AppActivity implements ViewContract.ViewView, BaseFragment.OnFragmentListener{
    private ViewContract.ViewPresenter mViewPresenter = null;
    private DrawerLayout drawer_material_design;
    private FrameLayout frame_material_design;
    private NavigationView navigation_view_material_design;
    private int select_position = -1;//默认加载

    @Override
    protected void onDestroy() {
        if (null != mViewPresenter) {
            mViewPresenter.destroy();
            mViewPresenter = null;
        }
        super.onDestroy();
    }


    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mViewPresenter = new ViewPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_material_design;
    }

    @Override
    public void initView() {
        this.drawer_material_design = (DrawerLayout) this.findViewById(R.id.drawer_material_design);
        this.frame_material_design = (FrameLayout) this.findViewById(R.id.frame_material_design);
        this.navigation_view_material_design = (NavigationView) this.findViewById(R.id.navigation_view_material_design);
        initDrawerLayout();
        switchFragment(Constants.FRAGMENT_RECYCLER_VIEW_TYPE_LIST);
//        initFragment(Constants.FRAGMENT_RECYCLER_VIEW_TYPE_LIST);
    }

    private void initDrawerLayout() {
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer_material_design, toolbar, R.string.drawer_open, R.string.drawer_close);
        actionBarDrawerToggle.syncState();
        drawer_material_design.addDrawerListener(actionBarDrawerToggle);
        setNavigationView();
    }


    private void setNavigationView() {
        navigation_view_material_design.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        int position = 0;
                        switch (menuItem.getItemId()) {
                            case R.id.navigation_item_list:
                                position = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_LIST;
                                break;
                            case R.id.navigation_item_grid:
                                position = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_GRID;
                                break;
                            case R.id.navigation_item_staggered:
                                position = Constants.FRAGMENT_RECYCLER_VIEW_TYPE_STAGGERED;
                                break;
                        }
                        menuItem.setChecked(true);
                        drawer_material_design.closeDrawers();
                        switchFragment(position);
//                        initFragment(position);
                        return true;
                    }
                });
    }

    private void switchFragment(int position) {
        if (position != select_position) {
            FragmentManager mFragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
            Fragment fragment_from = mFragmentManager.findFragmentByTag(Constants.FRAGMENT_RECYCLER_VIEW_TAG[position]);
            Fragment fragment_to = RecyclerViewFragment.newInstance(this, position);
            if (null == fragment_to) {
                LogHelp.d("Fragment空的");
            } else if (null != fragment_to) {
                LogHelp.d("Fragment不空");
            }
            if (null == fragment_from) {
                LogHelp.d("现在的Fragment空的");
            } else if (null != fragment_from) {
                LogHelp.d("现在的Fragment不空");
            }
            if (null != fragment_from && fragment_to.isAdded()) {
                fragmentTransaction.hide(fragment_from).show(fragment_to).commit();
                LogHelp.d("切换隐藏");
            } else if (null != fragment_from && !fragment_to.isAdded()) {
                fragmentTransaction.hide(fragment_from).add(R.id.frame_material_design, fragment_to, Constants.FRAGMENT_RECYCLER_VIEW_TAG[position]).commit();
                LogHelp.d("隐藏添加");
            } else {
                fragmentTransaction.add(R.id.frame_material_design, fragment_to, Constants.FRAGMENT_RECYCLER_VIEW_TAG[position]).commit();
                LogHelp.d("添加");
            }
            this.select_position = position;
        }
    }

    private void initFragment(int position) {
        select_position = position;
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        Fragment fragment = RecyclerViewFragment.newInstance(this, position);
        fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
        fragmentTransaction.replace(R.id.frame_material_design, fragment, Constants.FRAGMENT_RECYCLER_VIEW_TAG[position]);
        fragmentTransaction.commit();
    }

    @Override
    public void initData() {
        mViewPresenter.setData();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onFragmentClick(int fragment_id, boolean isOK, int position, Object object) {
        if (isOK) {
            switch (position) {
                case 0:
                    toastShow("点击图片");
                    break;
                case 1:
                    toastShow("点击文字");
                    break;
            }
        }
    }

}
